<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>还有多少天</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        body {
            height: 100vh;
            width: 100vw;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        main {
            width: 300px;
            padding: 20px 40px;
        }

        .show {
            height: 30px;
            line-height: 30px;
            font-size: 24px;
        }

        .show-day {
            color: red;
            display: inline-block;
            font-size: 28px;
            padding: 0 18px;
        }
        .inputs {
            margin-top: 20px;
        }
        .inputs label {
            font-size: 24px;
        }
        .inputs input {
            width: 100% ;
            font-size: 16px;
        }

        .tip {
            display: inline-block;
            height: 20px;
            line-height: 20px;
            font-size: 14px;
            color: red;
        }
    </style>
</head>
<body>
<main>
    <div class="show"></div>
    <div class="inputs">
        <div class="begin">
            <label>
                Begin:
                <input type="date">
            </label>
            <span class="tip"></span>
        </div>
        <div class="end">
            <label>
                End:
                <input type="date">
            </label>
            <span class="tip"></span>
        </div>
    </div>
</main>
<script>
    ;(function (){
        const show = document.querySelector('.show');
        const begin = document.querySelector('.begin input');
        const end = document.querySelector('.end input');
        const beginTip = document.querySelector('.begin .tip');
        const endTip = document.querySelector('.end .tip');
        end.addEventListener('change', function () {
            if (begin.value === ''){
                beginTip.textContent = '请填写开始时间！';
                return false;
            }
            clearTips(beginTip, endTip);
            show.innerHTML = beginToEnd(begin.value, end.value);
        }, false);
        begin.addEventListener('change', function () {
            if (end.value === ''){
                endTip.textContent = '请填写结束时间！';
                return false;
            }
            clearTips(beginTip, endTip);
            show.innerHTML = beginToEnd(begin.value, end.value);
        }, false);

        let clearTips = (...tips) => {
            tips.forEach(value => value.textContent = '')
        }

        let beginToEnd = (beginTime, endTime) => {
            let timeGap = new Date(endTime) - new Date(beginTime);
            let days = Number(timeGap / (1000 * 60 * 60 * 24)).toFixed(0);
            return `相距时间为:<span class="show-day">${days}</span>天`;
        }
    })();
</script>
</body>
</html>